package com.example.saifly.inPageRecord;

import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.common.Activity;
import com.example.common.Application.MyApplication;
import com.example.common.Util.FloatUtil;
import com.example.saifly.R;
import com.example.saifly.inPageRecord.RecordModel.BreathChartData;
import com.example.saifly.inPageRecord.RecordModel.BreathData;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import lecho.lib.hellocharts.formatter.LineChartValueFormatter;
import lecho.lib.hellocharts.formatter.SimpleLineChartValueFormatter;
import lecho.lib.hellocharts.gesture.ContainerScrollType;
import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Line;
import lecho.lib.hellocharts.model.LineChartData;
import lecho.lib.hellocharts.model.PointValue;
import lecho.lib.hellocharts.model.ValueShape;
import lecho.lib.hellocharts.view.LineChartView;

public class BreathActivity extends Activity {


    private Date date = new Date();
    private LineChartView lineChart;
    private List<PointValue> mPointValues = new ArrayList<PointValue>();
    private List<AxisValue> mAxisXValues = new ArrayList<AxisValue>();
    private List<BreathData> breathDataList=new ArrayList<>();
    private SharedPreferences sp= MyApplication.getContext().getSharedPreferences("config", Context.MODE_PRIVATE);
    private String nickName,time,portrait;
    private float vte;//潮气量
    private float rr;//呼吸频率
    private float leak;//漏气量
    private double pefr=0;//峰流速
    private TextView tvName,tvTime,tvScore,tvDeviceId,tvVte,tvRr,tvLeak,tvPefr;
    private String score;
    private CircleImageView ciImage;

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_breath;
    }

    @Override
    protected void init() {
        super.init();
        lineChart=findViewById(R.id.breath_chart);
        tvName=findViewById(R.id.breath_nickname);
        tvTime=findViewById(R.id.breath_time);
        tvScore=findViewById(R.id.breath_score);
        tvDeviceId=findViewById(R.id.breath_device_id);
        tvVte=findViewById(R.id.breath_tidal_value);
        tvRr=findViewById(R.id.breath_breath_value);
        ciImage=findViewById(R.id.profile_image);
        tvLeak=findViewById(R.id.breath_through_value);
        tvPefr=findViewById(R.id.breath_max_value);
        breathDataList= (List<BreathData>) getIntent().getSerializableExtra("dataList");
        score=getIntent().getStringExtra("breathScore");



        initData();


        getAxisPoints();//获取坐标点
        initLineChart();//初始化
    }

    private void initData(){
        //获取用户名

        nickName=sp.getString("nickname","用户");

        //获取头像
        String initialImageUrl = sp.getString("portrait","default");
        Glide.with(BreathActivity.this).load("https://lztoos.oss-cn-shenzhen.aliyuncs.com/"+initialImageUrl).into(ciImage);
        portrait=sp.getString("portrait","");

        for(int i=0;i<breathDataList.size();i++){
            vte+=breathDataList.get(i).getVte();
            rr+=breathDataList.get(i).getRr();
            leak+=breathDataList.get(i).getLeak();
            if(breathDataList.get(i).getPefr()>pefr){
                pefr=breathDataList.get(i).getPefr();
            }
        }
        int i=breathDataList.size();
        vte=vte/i;
        rr=rr/i;
        leak=leak/i;

        tvName.setText(nickName);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");// HH:mm:ss
        //sharedPreferences.edit().putString("time",simpleDateFormat.format(date)).apply();
        tvTime.setText(simpleDateFormat.format(date));

        tvScore.setText(score);
        Log.d("score", "initData: "+score);
        tvDeviceId.setText(breathDataList.get(0).getDeviceid());
        tvVte.setText(FloatUtil.holdDecimal(vte,2));
        tvRr.setText(FloatUtil.holdDecimal(rr,2));
        tvLeak.setText(FloatUtil.holdDecimal(leak,2));
        tvPefr.setText(FloatUtil.holdDecimal((float)pefr,2));

    }

    private void initLineChart() {


        LineChartValueFormatter chartValueFormatter = new SimpleLineChartValueFormatter(2);

        Line line = new Line(mPointValues).setColor(Color.parseColor("#FFCD41"));  //折线的颜色（橙色）
        List<Line> lines = new ArrayList<Line>();
        line.setShape(ValueShape.CIRCLE);//折线图上每个数据点的形状  这里是圆形 （有三种 ：ValueShape.SQUARE  ValueShape.CIRCLE  ValueShape.DIAMOND）
        line.setCubic(true);//曲线是否平滑，即是曲线还是折线
        line.setFilled(false);//是否填充曲线的面积
        line.setHasLabels(true);//曲线的数据坐标是否加上备注
        line.setFormatter(chartValueFormatter);//显示小数点
//      line.setHasLabelsOnlyForSelected(true);//点击数据坐标提示数据（设置了这个line.setHasLabels(true);就无效）
        line.setHasLines(true);//是否用线显示。如果为false 则没有曲线只有点显示
        line.setHasPoints(true);//是否显示圆点 如果为false 则没有原点只有点显示（每个数据点都是个大的圆点）
        lines.add(line);
        LineChartData data = new LineChartData();
        data.setLines(lines);

        //X轴的设置
        Axis axisX = new Axis(); //X轴
        axisX.setHasTiltedLabels(true);  //X坐标轴字体是斜的显示还是直的，true是斜的显示
        axisX.setTextColor(Color.rgb(17,150,238));  //设置字体颜色
        //axisX.setName("date");  //表格名称
        axisX.setTextSize(10);//设置字体大小
        axisX.setMaxLabelChars(5); //最多几个X轴坐标，意思就是你的缩放让X轴上数据的个数7<=x<=mAxisXValues.length
        axisX.setValues(mAxisXValues);  //填充X轴的坐标名称
        data.setAxisXBottom(axisX);
        //data.setAxisXTop(axisX);  //x 轴在顶部
        axisX.setHasLines(true); //x 轴分割线

        //Y轴的设置
        Axis axisY = new Axis();  //Y轴
        axisY.setName("");//y轴标注
        axisY.setTextSize(8);//设置字体大小
        data.setAxisYLeft(axisY);  //Y轴设置在左边

        //动态图表
        lineChart.setInteractive(false);
        lineChart.setZoomType(ZoomType.HORIZONTAL);
        lineChart.setContainerScrollEnabled(true, ContainerScrollType.HORIZONTAL);
        lineChart.setLineChartData(data);
        lineChart.setVisibility(View.VISIBLE);

    }

    private void getAxisPoints() {
        for (int i = 0; i < breathDataList.size(); i++) {
            mPointValues.add(new PointValue(i, breathDataList.get(i).getMv()));
            mAxisXValues.add(new AxisValue(i).setLabel(breathDataList.get(i).getSystime()));
        }
    }
}
